
<template>
    <div class="deliverynotice-list-page">
        <el-form :inline="true" :model="formInline" class="demo-form-inline" style="margin-bottom:10px;border-bottom:1px solid #ccc;">
            <el-form-item label="订单编号" class="selectMB10">
                <el-input  v-model="formInline.poCode" placeholder="输入PO编号"></el-input>
            </el-form-item>
            <el-form-item label="制单日期" class="selectMB10">
                <el-date-picker type="date" size="mini" :editable="false" v-model="formInline.createStartTime"  placeholder="请选择" @change="changeCreateStartTime()"></el-date-picker> 至
                <el-date-picker type="date" size="mini" :editable="false" v-model="formInline.createEndTime" placeholder="请选择" @change="changeCreateEndTime()"></el-date-picker>
            </el-form-item>
           <!-- <el-form-item label="PO状态" class="selectMB10">
                <el-select v-model="formInline.status">
                    <el-option label="已保存" value="已保存"></el-option>
                    <el-option label="已提交" value="已提交"></el-option>
                    <el-option label="已审核" value="已审核"></el-option>
                    <el-option label="已完成" value="已完成"></el-option>
                </el-select>
            </el-form-item>-->
            <el-form-item label="采购类别" class="selectMB10">
                <el-select v-model="formInline.procurementType">
                    <el-option label="普通采购" value="NA"></el-option>
                    <el-option label="国际采购" value="NC"></el-option>
                    <el-option label="同法人转储" value="UB"></el-option>
                    <el-option label="跨法人转储" value="ND"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="期望到货日期" class="selectMB10">
                <el-date-picker type="date" size="mini" :editable="false" v-model="formInline.hopeStartTime"  placeholder="请选择" @change="changeHopeStartTime()"></el-date-picker> 至
                <el-date-picker type="date" size="mini" :editable="false" v-model="formInline.hopeEndTime"  placeholder="请选择" @change="changeHopeEndTime()"></el-date-picker>
            </el-form-item>
            <el-form-item label="紧急程度" class="selectMB10">
                <el-select v-model="formInline.emergentDegree">
                    <el-option label="一般" value="NORMAL"></el-option>
                    <el-option label="紧急" value="EMERGENCY"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="供应商编码"  class="selectMB10">
                <el-input  v-model="formInline.partnerCode" placeholder="输入供应商编码"></el-input>
            </el-form-item>
            <el-form-item label="供应商名称"  class="selectMB10">
                <el-input  v-model="formInline.partnerName" placeholder="输入供应商名称信息"></el-input>
            </el-form-item>
           <!-- <el-form-item label="供应商名称" class="selectMB10">
                <el-select size="mini" clearable  v-model="formInline.partnerCode" placeholder="请选择" prop="supplierName">
                    <el-option v-for="vendor in formInline.supplierDropdown" :label="vendor.partnerName" :value="vendor.partnerCode"></el-option>
                </el-select>
            </el-form-item>-->
            <el-form-item class="selectMB10">
                <el-button class="bolder outsideBtnBS selectAll" :disabled="selectDisabled" size="mini" type="danger" @click="querySheet()">查询</el-button>
            </el-form-item>
        </el-form>
        <!--标签页-->
        <el-tabs v-model="activeName" type="card" @tab-click="handleClick">
            <el-tab-pane label="待送货明细" name="first">
                <p class="marginBot">
                    <!--<el-button class="bolder outsideBtnBS" size="mini" type="danger" @click="selectAll()">全选</el-button>-->
                    <el-button class="bolder outsideBtnBS" size="mini" type="danger"  @click="reverseSelection(deliveryPendingData)">反选</el-button>
                    <el-button class="bolder outsideBtnBS" size="mini" type="danger" @click="createDeliveryValid()" v-if="powerFlag">
                        <!--<router-link :to="{ path: 'CreateDeliverySheet', query: { paramDataList:shipRequestNos }}" >创建送货单</router-link>-->
                        创建送货单
                    </el-button>
                </p>
                <!--待送货明细表-->
                <el-table ref="multipleTable" :data="deliveryPendingData" border style="width: 100%" v-loading.body="loading"  @selection-change="handleSelectionChange">
                    <el-table-column type="index" header-align="center" align="center" label="序"  width="40"></el-table-column>
                    <el-table-column type="selection" align="center"  width="40"></el-table-column>
                    <el-table-column
                            prop="shipRequestNo"
                            header-align="center" align="center"
                            width="100"
                            label="送货通知单号" show-overflow-tooltip>
                    </el-table-column>
                    <el-table-column
                            prop="adressDetail"
                            header-align="center"
                            label="送货地"
                            width="140" show-overflow-tooltip>
                    </el-table-column>
                    <el-table-column
                            prop="poCode"
                            header-align="center"
                            label="PO"
                            width="100" show-overflow-tooltip>
                    </el-table-column>
                    <el-table-column
                            prop="currHopeTime"
                            header-align="center"
                            label="期望到货时间"
                            width="120" show-overflow-tooltip>
                    </el-table-column>
                    <el-table-column
                            prop="materialCode"
                            header-align="center"
                            label="备件编码"
                            width="110" show-overflow-tooltip>
                    </el-table-column>
                    <el-table-column
                            prop="materialName"
                            header-align="center"
                            label="备件名称"
                            width="110" show-overflow-tooltip>
                    </el-table-column>
                    <el-table-column
                            prop="standard"
                            header-align="center"
                            label="规格"
                            width="100" show-overflow-tooltip>
                    </el-table-column>
                    <el-table-column
                            prop="materialUnit"
                            header-align="center"
                            label="单位"
                            width="100" show-overflow-tooltip>
                    </el-table-column>
                    <el-table-column
                            prop="materialQuantity"
                            header-align="center"
                            label="通知数量"
                            width="100">
                    </el-table-column>
                    <el-table-column
                            prop="deliveredQuantity"
                            header-align="center"
                            label="已送货数量"
                            width="100">
                    </el-table-column>
                    <el-table-column
                            prop="notDeliveredQuantity"
                            header-align="center"
                            label="未送货数量"
                            width="100">
                    </el-table-column>
                </el-table>
                <!--分页-->
                <el-pagination v-if="isPaging" @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageIndex"  :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :page-count="totalPage" :total="totalNumber"></el-pagination>

            </el-tab-pane>
            <el-tab-pane label="已送货明细" name="second">
                <!--已送货明细表-->
                <el-table :data="deliveryedData" border style="width: 100%" v-loading.body="loading2">
                    <el-table-column
                            type="index"
                            header-align="center" align="center"
                            label="序"
                            width="24">
                    </el-table-column>
                    <el-table-column
                            prop="poCode"
                            header-align="center"
                            label="PO"
                            width="100" show-overflow-tooltip>
                    </el-table-column>
                    <el-table-column
                            prop="hopeTime"
                            header-align="center"
                            label="期望到货时间"
                            width="120" show-overflow-tooltip>
                    </el-table-column>
                    <el-table-column
                            prop="materialCode"
                            header-align="center"
                            label="备件编码"
                            width="110" show-overflow-tooltip>
                    </el-table-column>
                    <el-table-column
                            prop="materialName"
                            header-align="center"
                            label="备件名称"
                            width="110" show-overflow-tooltip>
                    </el-table-column>
                    <el-table-column
                            prop="standard"
                            header-align="center"
                            label="规格"
                            width="100" show-overflow-tooltip>
                    </el-table-column>
                    <el-table-column
                            prop="materialUnit"
                            header-align="center"
                            label="单位"
                            width="100" show-overflow-tooltip>
                    </el-table-column>
                    <el-table-column
                            prop="materialQuantity"
                            header-align="center"
                            label="PO数量"
                            width="100">
                    </el-table-column>
                    <el-table-column
                            prop="deliveredQuantity"
                            header-align="center"
                            label="已送货数量"
                            width="100">
                    </el-table-column>
                    <el-table-column
                            prop="notDeliveredQuantity"
                            header-align="center"
                            label="未送货数量"
                            width="100">
                    </el-table-column>
                    <el-table-column
                            prop="receivedQuantity"
                            header-align="center"
                            label="已收货数量"
                            width="100">
                    </el-table-column>
                </el-table>
                <!--分页-->
                <el-pagination v-if="isPaging2" @size-change="handleSizeChange2" @current-change="handleCurrentChange2" :current-page="pageIndex2"  :page-size="pageSize2" layout="total, sizes, prev, pager, next, jumper" :page-count="totalPage2" :total="totalNumber2"></el-pagination>

            </el-tab-pane>
        </el-tabs>
    </div>
</template>
<script>
    export default {
        data(){
            return{
                // 查询表单
                formInline:{
                    poCode:"",
                    createStartTime:"",
                    createEndTime:"",
                    status:"",
                    procurementType:"",
                    hopeStartTime:"",
                    hopeEndTime:"",
                    emergentDegree:"",
                    partnerCode:"",
                    partnerName:"",
                    supplierDropdown:[],//下拉列表获取供应商的信息
                    legalPersonId:""//查询供应商所需传的参数
                },
                activeName: 'first',
                deliveryPendingData:[],//待明细送货
                deliveryedData:[],//已明细送货

                selectedData:[],//选中的列表集合
                shipRequestNos:[],//需要创建送货单的通知发货单号集合

                seleType:"",//定义查询目标

                loading:false,
                loading2:false,

                //待送货 分页
                pageIndex:1,    //当前页（必传）
                pageSize :10,   //每页多少条
                totalNumber: 20,
                totalPage:2,
                isPaging:false,

                //已送货 分页
                pageIndex2:1,    //当前页（必传）
                pageSize2 :10,   //每页多少条
                totalNumber2: 20,
                totalPage2:2,
                isPaging2:false,
                //权限
                powerFlag:false,
                //查询禁用
                selectDisabled:false
            }
        },
        computed:{
            powerData(){
                return this.$store.state.powerData;
            }
        },
        watch:{
            powerData(){
                var _this=this;
                for(var i=0;i<_this.powerData.length;i++){
                    if(_this.powerData[i]=="ROLE_security_deliveryNotification_create"){
                        //送货通知创建送货单权限
                        _this.powerFlag=true;
                    }
                }
            }
        },
        //进入页面立即运行此方法
        mounted: function () {
            var _this = this;
            _this.$store.commit('currentActive', 'DeliveryNoticeSheet');
            // //ajax前后查询按钮禁用
            // $(".selectAll").ajaxStart(function(){
            //     _this.selectDisabled=true;
            // });
            // $(".selectAll").ajaxStop(function(){
            //     _this.selectDisabled=false;
            // });
            _this.queryDeliveryNoticeSheet();    //待送货明细列表表单查询
            //判断权限
            for(var i=0;i<_this.powerData.length;i++){
                if(_this.powerData[i]=="ROLE_security_deliveryNotification_create"){
                    //送货通知创建送货单权限
                    _this.powerFlag=true;
                }
            }
        },
        methods: {
            //转换日期格式
            switchDate(myDate){
                return myDate.getFullYear()+"-"+(myDate.getMonth()+1)+"-"+myDate.getDate();
            },
            //只有改变日期参数时，才改变日期格式
            changeCreateStartTime(){
                var _this=this;
                if(_this.formInline.createStartTime!=""){

                }
                _this.formInline.createStartTime=_this.switchDate(_this.formInline.createStartTime);
            },
            changeCreateEndTime(){
                var _this=this;
                if(_this.formInline.createEndTime!=""){
                    _this.formInline.createEndTime=_this.switchDate(_this.formInline.createEndTime);
                }
            },
            changeHopeStartTime(){
                var _this=this;
                if(_this.formInline.hopeStartTime!=""){
                    _this.formInline.hopeStartTime=_this.switchDate(_this.formInline.hopeStartTime);
                }
            },
            changeHopeEndTime(){
                var _this=this;
                if(_this.formInline.hopeEndTime!=""){
                    _this.formInline.hopeEndTime=_this.switchDate(_this.formInline.hopeEndTime);
                }
            },
            // 查询
            querySheet(){
                var _this=this;
                //分情况查询
                if(_this.seleType=="0"){
                    _this.queryDeliveryNoticeSheet();//待送货查询

                }else if( _this.seleType=="1"){
                    _this.queryDeliveryed();//已送货查询
                }
            },
            //供应商下拉列表值
            /*dropdownValue(){
                var _this=this;
                $.ajax({
                    type:'GET',
                    url:_this.metadata_service+"partner/queryByLegalPerson?legalPersonId="+_this.formInline.legalPersonId,
                    cache:false,
                    success:function(data){
                        if(data.success){
                            _this.formInline.supplierDropdown=data.data;
                        }else{
                            _this.common.errorTip(data.errorMessage);
                        }
                    }
                })
            },*/
            //待送货明细列表表单查询
            queryDeliveryNoticeSheet(){
                var _this=this;
                _this.loading=true;
                _this.loading2=false;
                var tmp={
                    poCode:_this.formInline.poCode,//po编号
                    createStartTime:_this.formInline.createStartTime,//制单日期(开始)
                    createEndTime:_this.formInline.createEndTime,  //制单日期(结束)
                    procurementType:_this.formInline.procurementType, //采购类别
                    hopeStartTime:_this.formInline.hopeStartTime,//期望到货日期(开始)
                    hopeEndTime:_this.formInline.hopeEndTime,//期望到货日期(结束)
                    emergentDegree:_this.formInline.emergentDegree,//紧急程度
                    partnerCode:_this.formInline.partnerCode,//供应商编码
                    partnerName:_this.formInline.partnerName,//供应商名称
                    pageIndex:_this.pageIndex,
                    pageSize:_this.pageSize
                };
                //console.log(JSON.stringify(tmp))
               //添加接口
                $.ajax({
                    type: 'POST',
                    url: _this.procurement_service+"shipNotice/findDeliverlist",
                    cache: false,
                    data:JSON.stringify(tmp),
                    dataType:'JSON',
                    contentType: "application/json;charset=utf-8",
                    success: function (data) {
                        //console.log(data)
                        //加一层权限验证，如果返回error,则返回空
                        if(data.success){
                            //有权限
                            // if (data.success) {
                            _this.loading=false;
                            _this.isPaging=true;
                            _this.totalPage=data.data.totalPage;
                            _this.totalNumber=data.data.totalNumber;
                            _this.deliveryPendingData=data.data.dataList;
                            _this.formInline.legalPersonId=data.data.legalPersonId;
                            //_this.dropdownValue();//获取供应商列表
                            //console.log(data.data.dataList);
                            /*}else{
                             _this.common.errorTip(data.errorMessage)
                             }*/
                        }else{
                            _this.loading=false;
                            _this.common.errorTip(data.errorMessage)
                        }

                    }
                });
            },
            // 待送货明细   反选
            reverseSelection(rows){
               // console.log(rows);
                var _this=this;
                if (rows) {
                    rows.forEach(row => {
                       // console.log(row)
                        _this.$refs.multipleTable.toggleRowSelection(row);
                    });
                } else {
                    //console.log(row+"---else---")
                    _this.$refs.multipleTable.clearSelection();
                }
            },
            // selection-change	当选择项发生变化时会触发该事件
            handleSelectionChange(val){
                var _this=this;
                //console.log(val);
                _this.selectedData=val;
                _this.shipRequestNos=[];
                for(var i=0;i<val.length;i++){
                    var parmsObj={};
                    parmsObj.shipRequestNo=val[i].shipRequestNo;
                    _this.shipRequestNos.push(parmsObj);
                }
                //console.log(_this.shipRequestNos)
            },
            //去首尾空格
            trimStr(str){
                return str.replace(/(^\s*)|(\s*$)/g,"");
            },
            //判断一个值在一个数组中是否存在
            isHas(arr,value){
                //console.log(arr)
                for(var i=0;i<arr.length;i++){
                    //console.log(arr[i]!=value)
                    if(arr[i]!=value){
                        //console.log("false")
                        return false;
                    }
                }
                //console.log("true")
                return true;
            },
            //创建送货单前的验证
            createDeliveryValid(){
                var _this=this;
                //验证送货地是否一致，一致则允许创建送货单，不一致准则不允许
                var arr=_this.selectedData;//_this.selectedData是被选中的列表集合
                var newArr=[];
                //console.log(arr);
                //arr长度必须大于1
                if(arr.length>0){
                    //循环判断选择的送货地是否一致
                    newArr.push(arr[0].adressDetail);
                    for(var i=1;i<=arr.length-1;i++){
                        arr[i].adressDetail=_this.trimStr(arr[i].adressDetail);//去掉arr[i]的首尾空格
                        //console.log(arr[i].adressDetail,_this.isHas(newArr,arr[i].adressDetail))
                        if(_this.isHas(newArr,arr[i].adressDetail)){
                            newArr.push(arr[i].adressDetail);
                        }else{
                            _this.common.errorTip("送货地不一致");
                            return;
                        }
                    }
                }else{
                    _this.common.errorTip("请选择送货地");
                    return;
                }
                //console.log("送货地一致");
                //验证通过后
                _this.$router.push({path:"/CreateDeliverySheet",query:{ paramDataList:_this.shipRequestNos }});
            },
            //已送货明细查询
            queryDeliveryed(){
                var _this=this;
                _this.loading2=true;
                _this.loading=false;
                var tmp={
                    poCode:_this.formInline.poCode,//po编号（必填）
                    createStartTime:_this.formInline.createStartTime,//制单日期(开始)（必填）
                    createEndTime:_this.formInline.createEndTime,  //制单日期(结束)（必填）
                    procurementType:_this.formInline.procurementType, //采购类别
                    hopeStartTime:_this.formInline.hopeStartTime,//期望到货日期(开始)（必填）
                    hopeEndTime:_this.formInline.hopeEndTime,//期望到货日期(结束)
                    emergentDegree:_this.formInline.emergentDegree,//紧急程度
                    partnerCode:_this.formInline.partnerCode,//供应商编码
                    partnerName:_this.formInline.partnerName,//供应商名称
                    pageIndex:_this.pageIndex2,
                    pageSize:_this.pageSize2
                };
                //添加接口
                $.ajax({
                    type: 'POST',
                    url: _this.procurement_service+"shipNotice/findSubmitNoticeList",
                    cache: false,
                    data:JSON.stringify(tmp),
                    dataType:'JSON',
                    contentType: "application/json;charset=utf-8",
                    success: function (data) {
                        //console.log(data)
                        //加一层权限验证，如果返回error,则返回空
                        if(data.success){
                            //有权限
                            // if (data.success) {
                            _this.loading2=false;
                            _this.isPaging2=true;
                            _this.totalPage2=data.data.totalPage;
                            _this.totalNumber2=data.data.totalNumber;
                            _this.deliveryedData=data.data.dataList;
                            _this.formInline.legalPersonId=data.data.legalPersonId;

                        }else{
                            //没有权限
                            _this.loading2=false;
                            _this.common.errorTip(data.errorMessage)
                        }

                    }
                });
            },
            //卡片点击
            handleClick(tab, event) {
                var _this=this;
               // console.log(tab.name)
                //判断是待送货明细还是已送货明细,first为待送货明细,second为已送货明细
                if(tab.name=="first"){
                    _this.seleType="0";//待送货
                    _this.queryDeliveryNoticeSheet();
                }else if(tab.name=="second"){
                    _this.seleType="1";//已送货
                    _this.queryDeliveryed();
                }
            },
            //待送货明细   分页
            handleSizeChange(val) {
                // console.log(`每页 ${val} 条`);
                var _this=this;
                _this.pageSize=val;
                _this.queryDeliveryNoticeSheet();

            },
            //点击页码分页查询点击事件
            handleCurrentChange(val) {
                // console.log(`当前页: ${val}`);
                var _this=this;
                _this.pageIndex=val;
                _this.queryDeliveryNoticeSheet();
            },
            // 已送货明细  分页
            handleSizeChange2(val) {
                // console.log(`每页 ${val} 条`);
                var _this=this;
                _this.pageSize2=val;
                _this.queryDeliveryed();

            },
            //点击页码分页查询点击事件
            handleCurrentChange2(val) {
                // console.log(`当前页: ${val}`);
                var _this=this;
                _this.pageIndex2=val;
                _this.queryDeliveryed();
            },

            // // ===================================
            // successTip(p) {     //成功时的提示信息
            //     this.$notify({
            //         title: '成功',
            //         message: p,
            //         type: 'success'
            //     });
            // },
            // errorTip(p) {     //错误提示
            //     this.$notify.error({
            //         title: '错误',
            //         message: p
            //     });
            // }
        }
    }
</script>
<style scoped>
    .marginBot{
        margin-bottom:10px;
    }
</style>

